package geppetto.cat.programs;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentEvaluator;
import geppetto.cat.alignments.AlignmentSymetrization;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.alignments.AlignmentEvaluator.Evaluation;
import geppetto.cat.corpus.BilingualCorpus;
import geppetto.cat.models.AbstractModel;

import java.io.IOException;



/**
 * Reads two alignments models generates the alignments
 * and symmetryzes and saves them.
 * @author javg
 *
 */
public class GetSymmetrizationEvals {
	
	public static void getSymmetrizationEvals(BilingualCorpus corpus, 
			byte sentenceSource, boolean projectPosteriors, 
			AbstractModel modelF,AbstractModel modelB, String grepString,
			String type){
		
		
		
		int[] sentences = new int[corpus.getNumSentences(sentenceSource)];
		for (int i = 0; i < sentences.length; i++) {
			sentences[i] = i;
		}
		AlignmentsSet predictedF = modelF.posteriorAlignments(sentences,
				sentenceSource, 1f , projectPosteriors,false);
		AlignmentsSet predictedB = modelB.posteriorAlignments(sentences,
				sentenceSource, 1f , projectPosteriors,false);
		
		for(int i = 0; i < predictedF.size(); i++){
			predictedF.get(i).resetPoints();
			predictedB.get(i).resetPoints();
		}
		
		for (double threshold = 0; threshold <= 0.8; threshold +=0.1) {
			AlignmentsSet predicted = null;
			if(type.equalsIgnoreCase("softIntersection")){
				predicted = AlignmentSymetrization.softIntersection(predictedF, predictedB, threshold);			
			}else if(type.equalsIgnoreCase("softUnion")){
				predicted = AlignmentSymetrization.softUnion(predictedF, predictedB, threshold);
			}else{
				System.out.println("Uknown type exiting");
				System.exit(-1);
			}
			
			
			System.out.println("Evals + " + threshold + AlignmentEvaluator.evaluate(predicted, corpus.getGold()));
			Evaluation[] broken = AlignmentEvaluator.evaluateRareWords(predicted, corpus.getGold(), corpus);
			System.out.println("Evals Rare + " + threshold + broken[0]);
			System.out.println("Evals Common + " + threshold + broken[1]);
			for(Alignment a : predicted._alignments){
				a.resetPoints();
			}
		}
	}

	
	
	
	public static void main(String[] args) throws IOException {
		String corpusDescription = args[0];
		int size = Integer.parseInt(args[1]);
		int maxSentenceSize = Integer.parseInt(args[2]);
		
		System.out.println("Corpus " + corpusDescription);
		System.out.println("Size " + size);
		System.out.println("Max Sent length " + maxSentenceSize);
		
		BilingualCorpus forwardCorpus = BilingualCorpus.getCorpusFromFileDescription(corpusDescription, size,maxSentenceSize);
		BilingualCorpus backwardCorpus = forwardCorpus.reverse();	
	
		String modelDir1 = args[3];
		String modelName1 = args[4];
		double epsilon1 = Double.parseDouble(args[5]);
		double slack1 = Double.parseDouble(args[6]);
		int maxStep1 = Integer.parseInt(args[7]);
		int maxProjecIter1 = Integer.parseInt(args[8]);
		String intersectionType = args[9];
		modelDir1 = modelDir1 + "/" + modelName1 + "/" + forwardCorpus.getName()
		+ "/" + size + "/model/";

		System.out.println("Model1 ");
		System.out.println("Model Dir " + modelDir1);
		System.out.println("Model Name " + modelName1);
		System.out.println("Epsilon " + epsilon1);
		System.out.println("Slack " + slack1);
		System.out.println("Max Step " + maxStep1);
		System.out.println("Max Project Iterations " + maxProjecIter1);
		
		AbstractModel[] models= GenericMethods.initializeModels(forwardCorpus, backwardCorpus, modelName1, modelDir1, 
				epsilon1, slack1, maxStep1, maxProjecIter1);
	
		getSymmetrizationEvals(forwardCorpus, BilingualCorpus.TEST_CORPUS, true, models[0], models[1], "aa", intersectionType);
		
		
		
	}

}
